package com.cswangdao.service.impl;

import com.cswangdao.domain.dto.AdPageDTO;
import com.cswangdao.domain.model.MarketAd;
import com.cswangdao.domain.model.MarketComment;
import com.cswangdao.mapper.MarketAdMapper;
import com.cswangdao.mapper.MarketCommentMapper;
import com.cswangdao.result.PageResult;
import com.cswangdao.service.AdService;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AdServiceImpl implements AdService {

    @Autowired
    private MarketAdMapper adMapper;

    @Override
    public PageResult<MarketAd> pageQueryInfo(AdPageDTO pageDTO) {
        try /*(SqlSession sqlSession = MybatisUtil.getSqlSession(true);)*/ {
         //   MarketAdMapper adMapper = sqlSession.getMapper(MarketAdMapper.class);
            PageHelper.startPage(pageDTO.getPage(), pageDTO.getLimit());
            Page<MarketAd> commentPage = adMapper.pageQueryInfo(pageDTO);
            PageResult<MarketAd> result = PageResult.result(commentPage);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public MarketAd addAd(MarketAd ad) {
        try/*(SqlSession sqlSession = MybatisUtil.getSqlSession();)*/ {
            Date now = new Date();
            ad.setAddTime(now);
            ad.setUpdateTime(now);
        //    MarketAdMapper adMapper = sqlSession.getMapper(MarketAdMapper.class);
            adMapper.addAd(ad);
          //  sqlSession.commit();
            return ad;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public MarketAd updateById(MarketAd ad) {
        try/*( SqlSession sqlSession = MybatisUtil.getSqlSession();)*/ {
     //       MarketAdMapper adMapper = sqlSession.getMapper(MarketAdMapper.class);
            adMapper.updateById(ad);
        //    sqlSession.commit();
            return ad;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteById(Integer id) {
        try /*(SqlSession sqlSession = MybatisUtil.getSqlSession();)*/{
          //  MarketAdMapper adMapper = sqlSession.getMapper(MarketAdMapper.class);
            adMapper.deleteById(id);
          //  sqlSession.commit();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}
